home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / obstack.c < prev    next >
C/C++ Source or Header  |  1994-02-09  |  15KB  |  490 lines

  1. /* obstack.c - subroutines used implicitly by object stack macros
  2.    Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4. This file is part of the GNU C Library.  Its master source is NOT part of
  5. the C library, however.  The master source lives in /gd/gnu/lib.
  6.  
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11.  
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with the GNU C Library; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. #include "obstack.h"
  23.  
  24. /* This is just to get __GNU_LIBRARY__ defined.  */
  25. #include <stdio.h>
  26.  
  27. /* Comment out all this code if we are using the GNU C Library, and are not
  28.    actually compiling the library itself.  This code is part of the GNU C
  29.    Library, but also included in many other GNU distributions.  Compiling
  30.    and linking in this code is a waste when using the GNU C library
  31.    (especially if it is a shared library).  Rather than having every GNU
  32.    program understand `configure --with-gnu-libc' and omit the object files,
  33.    it is simpler to just do this in the source for each such file.  */
  34.  
  35. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  36.  
  37.  
  38. #ifdef __STDC__
  39. #define POINTER void *
  40. #else
  41. #define POINTER char *
  42. #endif
  43.  
  44. /* Determine default alignment.  */
  45. struct fooalign {char x; double d;};
  46. #define DEFAULT_ALIGNMENT  \
  47.   ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0))
  48. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  49.    But in fact it might be less smart and round addresses to as much as
  50.    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
  51. union fooround {long x; double d;};
  52. #define DEFAULT_ROUNDING (sizeof (union fooround))
  53.  
  54. /* When we copy a long block of data, this is the unit to do it with.
  55.    On some machines, copying successive ints does not work;
  56.    in such a case, redefine COPYING_UNIT to `long' (if that works)
  57.    or `char' as a last resort.  */
  58. #ifndef COPYING_UNIT
  59. #define COPYING_UNIT int
  60. #endif
  61.  
  62. /* The non-GNU-C macros copy the obstack into this global variable
  63.    to avoid multiple evaluation.  */
  64.  
  65. struct obstack *_obstack;
  66.  
  67. /* Define a macro that either calls functions with the traditional malloc/free
  68.    calling interface, or calls functions with the mmalloc/mfree interface
  69.    (that adds an extra first argument), based on the state of use_extra_arg.
  70.    For free, do not use ?:, since some compilers, like the MIPS compilers,
  71.    do not allow (expr) ? void : void.  */
  72.  
  73. #define CALL_CHUNKFUN(h, size) \
  74.   (((h) -> use_extra_arg) \
  75.    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
  76.    : (*(h)->chunkfun) ((size)))
  77.  
  78. #define CALL_FREEFUN(h, old_chunk) \
  79.   do { \
  80.     if ((h) -> use_extra_arg) \
  81.       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
  82.     else \
  83.       (*(h)->freefun) ((old_chunk)); \
  84.   } while (0)
  85.  
  86.  
  87. /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
  88.    Objects start on multiples of ALIGNMENT (0 means use default).
  89.    CHUNKFUN is the function to use to allocate chunks,
  90.    and FREEFUN the function to free them.
  91.  
  92.    Return nonzero if successful, zero if out of memory.
  93.    To recover from an out of memory error,
  94.    free up some memory, then call this again.  */
  95.  
  96. int
  97. _obstack_begin (h, size, alignment, chunkfun, freefun)
  98.      struct obstack *h;
  99.      int size;
  100.      int alignment;
  101.      POINTER (*chunkfun) ();
  102.      void (*freefun) ();
  103. {
  104.   register struct _obstack_chunk* chunk; /* points to new chunk */
  105.  
  106.   if (alignment == 0)
  107.     alignment = DEFAULT_ALIGNMENT;
  108.   if (size == 0)
  109.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  110.     {
  111.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  112.      Use the values for range checking, because if range checking is off,
  113.      the extra bytes won't be missed terribly, but if range checking is on
  114.      and we used a larger request, a whole extra 4096 bytes would be
  115.      allocated.
  116.  
  117.      These number are irrelevant to the new GNU malloc.  I suspect it is
  118.      less sensitive to the size of the request.  */
  119.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  120.             + 4 + DEFAULT_ROUNDING - 1)
  121.            & ~(DEFAULT_ROUNDING - 1));
  122.       size = 4096 - extra;
  123.     }
  124.  
  125.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  126.   h->freefun = freefun;
  127.   h->chunk_size = size;
  128.   h->alignment_mask = alignment - 1;
  129.   h->use_extra_arg = 0;
  130.  
  131.   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  132.   if (!chunk)
  133.     {
  134.       h->alloc_failed = 1;
  135.       return 0;
  136.     }
  137.   h->alloc_failed = 0;
  138.   h->next_free = h->object_base = chunk->contents;
  139.   h->chunk_limit = chunk->limit
  140.     = (char *) chunk + h->chunk_size;
  141.   chunk->prev = 0;
  142.   /* The initial chunk now contains no empty object.  */
  143.   h->maybe_empty_object = 0;
  144.   return 1;
  145. }
  146.  
  147. int
  148. _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
  149.      struct obstack *h;
  150.      int size;
  151.      int alignment;
  152.      POINTER (*chunkfun) ();
  153.      void (*freefun) ();
  154.      POINTER arg;
  155. {
  156.   register struct _obstack_chunk* chunk; /* points to new chunk */
  157.  
  158.   if (alignment == 0)
  159.     alignment = DEFAULT_ALIGNMENT;
  160.   if (size == 0)
  161.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  162.     {
  163.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  164.      Use the values for range checking, because if range checking is off,
  165.      the extra bytes won't be missed terribly, but if range checking is on
  166.      and we used a larger request, a whole extra 4096 bytes would be
  167.      allocated.
  168.  
  169.      These number are irrelevant to the new GNU malloc.  I suspect it is
  170.      less sensitive to the size of the request.  */
  171.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  172.             + 4 + DEFAULT_ROUNDING - 1)
  173.            & ~(DEFAULT_ROUNDING - 1));
  174.       size = 4096 - extra;
  175.     }
  176.  
  177.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  178.   h->freefun = freefun;
  179.   h->chunk_size = size;
  180.   h->alignment_mask = alignment - 1;
  181.   h->extra_arg = arg;
  182.   h->use_extra_arg = 1;
  183.  
  184.   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
  185.   if (!chunk)
  186.     {
  187.       h->alloc_failed = 1;
  188.       return 0;
  189.     }
  190.   h->alloc_failed = 0;
  191.   h->next_free = h->object_base = chunk->contents;
  192.   h->chunk_limit = chunk->limit
  193.     = (char *) chunk + h->chunk_size;
  194.   chunk->prev = 0;
  195.   /* The initial chunk now contains no empty object.  */
  196.   h->maybe_empty_object = 0;
  197.   return 1;
  198. }
  199.  
  200. /* Allocate a new current chunk for the obstack *H
  201.    on the assumption that LENGTH bytes need to be added
  202.    to the current object, or a new object of length LENGTH allocated.
  203.    Copies any partial object from the end of the old chunk
  204.    to the beginning of the new one.  */
  205.  
  206. void
  207. _obstack_newchunk (h, length)
  208.      struct obstack *h;
  209.      int length;
  210. {
  211.   register struct _obstack_chunk*    old_chunk = h->chunk;
  212.   register struct _obstack_chunk*    new_chunk;
  213.   register long    new_size;
  214.   register int obj_size = h->next_free - h->object_base;
  215.   register int i;
  216.   int already;
  217.  
  218.   /* Compute size for new chunk.  */
  219.   new_size = (obj_size + length) + (obj_size >> 3) + 100;
  220.   if (new_size < h->chunk_size)
  221.     new_size = h->chunk_size;
  222.  
  223.   /* Allocate and initialize the new chunk.  */
  224.   new_chunk = CALL_CHUNKFUN (h, new_size);
  225.   if (!new_chunk)
  226.     {
  227.       h->alloc_failed = 1;
  228.       return;
  229.     }
  230.   h->alloc_failed = 0;
  231.   h->chunk = new_chunk;
  232.   new_chunk->prev = old_chunk;
  233.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  234.  
  235.   /* Move the existing object to the new chunk.
  236.      Word at a time is fast and is safe if the object
  237.      is sufficiently aligned.  */
  238.   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  239.     {
  240.       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  241.        i >= 0; i--)
  242.     ((COPYING_UNIT *)new_chunk->contents)[i]
  243.       = ((COPYING_UNIT *)h->object_base)[i];
  244.       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  245.      but that can cross a page boundary on a machine
  246.      which does not do strict alignment for COPYING_UNITS.  */
  247.       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  248.     }
  249.   else
  250.     already = 0;
  251.   /* Copy remaining bytes one by one.  */
  252.   for (i = already; i < obj_size; i++)
  253.     new_chunk->contents[i] = h->object_base[i];
  254.  
  255.   /* If the object just copied was the only data in OLD_CHUNK,
  256.      free that chunk and remove it from the chain.
  257.      But not if that chunk might contain an empty object.  */
  258.   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
  259.     {
  260.       new_chunk->prev = old_chunk->prev;
  261.       CALL_FREEFUN (h, old_chunk);
  262.     }
  263.  
  264.   h->object_base = new_chunk->contents;
  265.   h->next_free = h->object_base + obj_size;
  266.   /* The new chunk certainly contains no empty object yet.  */
  267.   h->maybe_empty_object = 0;
  268. }
  269.  
  270. /* Return nonzero if object OBJ has been allocated from obstack H.
  271.    This is here for debugging.
  272.    If you use it in a program, you are probably losing.  */
  273.  
  274. #ifdef __STDC__
  275. /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
  276.    obstack.h because it is just for debugging.  */
  277. int _obstack_allocated_p (struct obstack *h, POINTER obj);
  278. #endif
  279.  
  280. int
  281. _obstack_allocated_p (h, obj)
  282.      struct obstack *h;
  283.      POINTER obj;
  284. {
  285.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  286.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  287.  
  288.   lp = (h)->chunk;
  289.   /* We use >= rather than > since the object cannot be exactly at
  290.      the beginning of the chunk but might be an empty object exactly
  291.      at the end of an adjacent chunk. */
  292.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  293.     {
  294.       plp = lp->prev;
  295.       lp = plp;
  296.     }
  297.   return lp != 0;
  298. }
  299.  
  300. /* Free objects in obstack H, including OBJ and everything allocate
  301.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  302.  
  303. #undef obstack_free
  304.  
  305. /* This function has two names with identical definitions.
  306.    This is the first one, called from non-ANSI code.  */
  307.  
  308. void
  309. _obstack_free (h, obj)
  310.      struct obstack *h;
  311.      POINTER obj;
  312. {
  313.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  314.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  315.  
  316.   lp = h->chunk;
  317.   /* We use >= because there cannot be an object at the beginning of a chunk.
  318.      But there can be an empty object at that address
  319.      at the end of another chunk.  */
  320.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  321.     {
  322.       plp = lp->prev;
  323.       CALL_FREEFUN (h, lp);
  324.       lp = plp;
  325.       /* If we switch chunks, we can't tell whether the new current
  326.      chunk contains an empty object, so assume that it may.  */
  327.       h->maybe_empty_object = 1;
  328.     }
  329.   if (lp)
  330.     {
  331.       h->object_base = h->next_free = (char *)(obj);
  332.       h->chunk_limit = lp->limit;
  333.       h->chunk = lp;
  334.     }
  335.   else if (obj != 0)
  336.     /* obj is not in any of the chunks! */
  337.     abort ();
  338. }
  339.  
  340. /* This function is used from ANSI code.  */
  341.  
  342. void
  343. obstack_free (h, obj)
  344.      struct obstack *h;
  345.      POINTER obj;
  346. {
  347.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  348.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  349.  
  350.   lp = h->chunk;
  351.   /* We use >= because there cannot be an object at the beginning of a chunk.
  352.      But there can be an empty object at that address
  353.      at the end of another chunk.  */
  354.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  355.     {
  356.       plp = lp->prev;
  357.       CALL_FREEFUN (h, lp);
  358.       lp = plp;
  359.       /* If we switch chunks, we can't tell whether the new current
  360.      chunk contains an empty object, so assume that it may.  */
  361.       h->maybe_empty_object = 1;
  362.     }
  363.   if (lp)
  364.     {
  365.       h->object_base = h->next_free = (char *)(obj);
  366.       h->chunk_limit = lp->limit;
  367.       h->chunk = lp;
  368.     }
  369.   else if (obj != 0)
  370.     /* obj is not in any of the chunks! */
  371.     abort ();
  372. }
  373.  
  374. #if 0
  375. /* These are now turned off because the applications do not use it
  376.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  377.  
  378. /* Now define the functional versions of the obstack macros.
  379.    Define them to simply use the corresponding macros to do the job.  */
  380.  
  381. #ifdef __STDC__
  382. /* These function definitions do not work with non-ANSI preprocessors;
  383.    they won't pass through the macro names in parentheses.  */
  384.  
  385. /* The function names appear in parentheses in order to prevent
  386.    the macro-definitions of the names from being expanded there.  */
  387.  
  388. POINTER (obstack_base) (obstack)
  389.      struct obstack *obstack;
  390. {
  391.   return obstack_base (obstack);
  392. }
  393.  
  394. POINTER (obstack_next_free) (obstack)
  395.      struct obstack *obstack;
  396. {
  397.   return obstack_next_free (obstack);
  398. }
  399.  
  400. int (obstack_object_size) (obstack)
  401.      struct obstack *obstack;
  402. {
  403.   return obstack_object_size (obstack);
  404. }
  405.  
  406. int (obstack_room) (obstack)
  407.      struct obstack *obstack;
  408. {
  409.   return obstack_room (obstack);
  410. }
  411.  
  412. void (obstack_grow) (obstack, pointer, length)
  413.      struct obstack *obstack;
  414.      POINTER pointer;
  415.      int length;
  416. {
  417.   obstack_grow (obstack, pointer, length);
  418. }
  419.  
  420. void (obstack_grow0) (obstack, pointer, length)
  421.      struct obstack *obstack;
  422.      POINTER pointer;
  423.      int length;
  424. {
  425.   obstack_grow0 (obstack, pointer, length);
  426. }
  427.  
  428. void (obstack_1grow) (obstack, character)
  429.      struct obstack *obstack;
  430.      int character;
  431. {
  432.   obstack_1grow (obstack, character);
  433. }
  434.  
  435. void (obstack_blank) (obstack, length)
  436.      struct obstack *obstack;
  437.      int length;
  438. {
  439.   obstack_blank (obstack, length);
  440. }
  441.  
  442. void (obstack_1grow_fast) (obstack, character)
  443.      struct obstack *obstack;
  444.      int character;
  445. {
  446.   obstack_1grow_fast (obstack, character);
  447. }
  448.  
  449. void (obstack_blank_fast) (obstack, length)
  450.      struct obstack *obstack;
  451.      int length;
  452. {
  453.   obstack_blank_fast (obstack, length);
  454. }
  455.  
  456. POINTER (obstack_finish) (obstack)
  457.      struct obstack *obstack;
  458. {
  459.   return obstack_finish (obstack);
  460. }
  461.  
  462. POINTER (obstack_alloc) (obstack, length)
  463.      struct obstack *obstack;
  464.      int length;
  465. {
  466.   return obstack_alloc (obstack, length);
  467. }
  468.  
  469. POINTER (obstack_copy) (obstack, pointer, length)
  470.      struct obstack *obstack;
  471.      POINTER pointer;
  472.      int length;
  473. {
  474.   return obstack_copy (obstack, pointer, length);
  475. }
  476.  
  477. POINTER (obstack_copy0) (obstack, pointer, length)
  478.      struct obstack *obstack;
  479.      POINTER pointer;
  480.      int length;
  481. {
  482.   return obstack_copy0 (obstack, pointer, length);
  483. }
  484.  
  485. #endif /* __STDC__ */
  486.  
  487. #endif /* 0 */
  488.  
  489. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  490.